package SpilitFileProject;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class SplitFileMethod {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.printf("请输入操作(s:拆分,m:合并):");
        String cmd = s.next();
        System.out.printf("请输入原文件路径：");
        String filePath = s.next();
        if(cmd.equals("m")){
            mergeFile(filePath);
        }else if(cmd.equals("s")){
            System.out.printf("请输入拆分子文件的大小(kB)：");
            int perSize = s.nextInt()*1024;
            splitFile(new File(filePath),perSize);
        }else
            System.out.println("未知操作");

    }
    private static byte[] mergeArray(byte[] first,byte[] second){
        byte[] temp = Arrays.copyOf(first,first.length+second.length);
        byte[] result = Arrays.copyOfRange(temp,first.length,temp.length);
        return result;
    }
    public static void mergeFile(String[] files){

        for(String fname:files){
            File f = new File(fname);
            if(f.exists()){
                try (FileInputStream fis = new FileInputStream(f);//append
                     FileOutputStream fos = new FileOutputStream(new File(files[0].substring(0,files[0].length()-2)),true)){
                    byte[] temp = new byte[(int)f.length()];
                    fis.read(temp);
                    fos.write(temp);
                    fos.flush();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }

        }
    }
    public static void mergeFile(String oriFileName){
        File perFile = new File(oriFileName+"-0");
        FileOutputStream fos = null;
        FileInputStream fis = null;
        try{
            File oriFile = new File(oriFileName);
            fos = new FileOutputStream(oriFile);
            for(int i = 1;perFile.exists();i++) {
                byte[] temp = new byte[(int) perFile.length()];
                fis = new FileInputStream(perFile);
                fis.read(temp);
                fos.write(temp);
                fos.flush();
                System.out.printf("把文件%s输出至目标文件中%n",oriFileName+"-"+i);
                perFile = new File(oriFileName+"-"+i);
            }
            System.out.println("输出文件大小为:"+oriFile.length()+"字节");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(fis!=null){
                try {
                    fis.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try{
                    fos.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }

        }
        }
    public static void splitFile(File src,int perSize){
        if(src.length()==0)
            throw new RuntimeException("文件大小为0，不可拆分");
        byte[] fileContent = new byte[(int)src.length()];

        try(FileInputStream fis = new FileInputStream(src)){//try里自动关闭流
            fis.read(fileContent);
        }catch (IOException e){
            e.printStackTrace();
        }
        //读取
        //计算需要划分的数量
        int fileNumber;
        if(fileContent.length % perSize ==0){//能整除
            fileNumber = (int)(fileContent.length / perSize);
        }else {
            fileNumber = (int)(fileContent.length / perSize)+1;
        }
        for(int i = 0;i<fileNumber;i++){
            String eachName = src.getName()+"-"+i;
            File eachFile = new File(src.getParent(),eachName);
            byte[] eachContent;
            if(i != fileNumber-1) //不是最后一个
                eachContent = Arrays.copyOfRange(fileContent,perSize * i,perSize *(i+1));
            else
                eachContent = Arrays.copyOfRange(fileContent,perSize*i,fileContent.length);
            FileOutputStream fos=null;
            try{
                fos = new FileOutputStream(eachFile);
                fos.write(eachContent);
                System.out.printf("输出子文件%s,大小为:%d 字节.%n",eachFile.getAbsoluteFile(),eachFile.length());
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                if(fos!=null){
                    try {
                        fos.close();
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
